﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Avatar.ResumeService;
using Avatar.Model;
using AvatarHelper;
using System.Data.Linq;
using Avatar.MembershipService;

public partial class PersonLanguage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PersonHelper.BasicInformationJobIntentionCheck();

        if (!IsPostBack)
        {
            if (Membership.GetUser() != null)
            {
                using(var _adc=DBHelper.GetDataContext())
                {
                    DropDownListHelper.Bind(ddl_Language, _adc.Language.ToList(), "langname", "ID");
                    DropDownListHelper.Bind(ddl_Ability, _adc.Ability.ToList(), "name", "ID");
                    DropDownListHelper.Bind(ddl_Read, _adc.Ability.ToList(), "name", "ID");
                    DropDownListHelper.Bind(ddl_Write, _adc.Ability.ToList(), "name", "ID");
                    DropDownListHelper.Bind(ddl_English, _adc.EnglishLevel.ToList(), "Level", "ID");
                    DropDownListHelper.Bind(ddl_Japanese, _adc.JapaneseLevel.ToList(), "Level", "ID");

                    var _resume = _adc.Resume.Where(r => r.User == Membership.GetUser().UserName).FirstOrDefault();

                    if (_resume != null)
                    {
                        ListView1.DataSource = _resume.LanguageAbility;
                        ListView1.DataBind();

                        var _languageLevel = _resume.LanguageLevel;

                        if (_languageLevel != null)
                        {
                            lbl_ID.Value = _languageLevel.ID.ToString();
                            ddl_English.SelectedValue = _languageLevel.EnglishLevel.ID.ToString();
                            ddl_Japanese.SelectedValue = _languageLevel.JapaneseLevel.ID.ToString();
                            txt_TOEFL.Text = _languageLevel.TOEFL.ToString();
                            txt_GRE.Text = _languageLevel.GRE.ToString();
                            txt_GMAT.Text = _languageLevel.GMAT.ToString();
                            txt_IELTS.Text = _languageLevel.IELTS.ToString();
                        }
                    }
                }
            }
            lbl_Language.Visible = false;
            ddl_Language.Visible = true;
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            Button _btn = sender as Button;
            Guid _id = string.IsNullOrEmpty(_btn.CommandArgument) ? Guid.Empty : new Guid(_btn.CommandArgument);
            LanguageAbilityManager.Delete(_id);

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        Button _btn = sender as Button;
        Guid _id = string.IsNullOrEmpty(_btn.CommandArgument) ?Guid.Empty : new Guid(_btn.CommandArgument);
        using (var _adc = DBHelper.GetDataContext())
        {
            var _e = _adc.LanguageAbility.Where(a => a.ID == _id).FirstOrDefault();
            if (_e != null)
            {
                txt_ID.Value = _e.ID.ToString();
                ddl_Language.SelectedValue = _e.LanguageID.ToString();
                ddl_Language.Visible = false;
                lbl_Language.Visible = true;
                lbl_Language.Text = _e.Language.langname;
                ddl_Ability.SelectedValue = _e.AbilityID.ToString();
                ddl_Read.SelectedValue = _e.Read.ToString();
                ddl_Write.SelectedValue = _e.Write.ToString();
            }
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            this.Save();

            Response.Redirect("./PersonResume.aspx");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }

    protected void btn_SaveNew_Click(object sender, EventArgs e)
    {
        try
        {
            this.Save();

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void Save()
    {
        try
        {
            Guid _id = string.IsNullOrEmpty(txt_ID.Value) ? Guid.Empty : new Guid(txt_ID.Value);
            var _user = Membership.GetUser();
            LanguageAbilityManager.SaveLanguageAbility(
                _id,
                Convert.ToInt32(ddl_Ability.SelectedValue),
                Convert.ToInt32(ddl_Language.SelectedValue),
                Convert.ToInt32(ddl_Read.SelectedValue),
                Convert.ToInt32(ddl_Write.SelectedValue),
                _user.UserName
                );

        }
        catch (Exception _e)
        {
            throw new Exception(_e.Message);
        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            //int _id = Request.QueryString["ID"] == null ? 0 : Convert.ToInt32(Request.QueryString["ID"].ToString());
            var _user = LoginManager.GetCurrentUser();
            if (_user != null)
            {
                LanguageLevelManager.SaveLanguageLevel(
                    _user.UserName,
                    Convert.ToInt32(ddl_English.SelectedValue),
                    Convert.ToInt32(ddl_Japanese.SelectedValue),
                    txt_TOEFL.Text,
                    txt_GRE.Text,
                    txt_GMAT.Text,
                    txt_IELTS.Text);
            }

            Response.Redirect("./PersonResume.aspx");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("./PersonResume.aspx");
    }
    protected void btn_LanguageLevelDelete_Click(object sender, EventArgs e)
    {
        try
        {
            Guid _id = string.IsNullOrEmpty(lbl_ID.Value) ? Guid.Empty : new Guid(lbl_ID.Value);
            LanguageLevelManager.Delete(_id);
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
}
